﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.DAL;
using rock.UI.common;
using rock.BLL;
using rock.common;

namespace rock.UI.fee
{
    public partial class UserControlFeeShaoYajing : UserControl
    {

        DalFEE_SHAOYAJING dal = new DalFEE_SHAOYAJING();
        BllFEE_SHAOYAJING aBllFEE_SHAOYAJING = new BllFEE_SHAOYAJING();
        DalFEE_TUIYAJING aDalFEE_TUIYAJING = new DalFEE_TUIYAJING();


        private int CURRENT_PAGE = 1;
        int XiaoQuSeqid = 0;
        int DongSeqid = 0;


        public UserControlFeeShaoYajing()
        {
            InitializeComponent();
           // this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
           // this.dataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
           this.userControlLeftTree1.selectedTreeinfo = selectedTreeinfo;
            this.userControlCommonListPage1.pageClick = refreshData;

            ViewUtils.setDataGridViewStlye(this.dataGridView1);
            ViewUtils.setDataGridViewStlye(this.dataGridView2);

            this.dateTimePicker1.Checked = false;
            this.dateTimePicker2.Checked = false;
        }



        //刷新数据
        public void refreshData(int goPage)
        {


            CURRENT_PAGE = goPage;
            String search = this.textBox1.Text;

            StringBuilder where = new StringBuilder(
                 "(" +
                         " HOUSE_ROOM.HOUSE_NUMBER LIKE '%" + search + "%' " +
                " OR " + " HOUSE_ROOM.HOUSE_NUMBER LIKE '%" + search + "%' " +
                " OR " + " RENT_CUSTOM.NAME LIKE '%" + search + "%' " +
                " OR " + " TEMP.XIAOQU_NAME LIKE '%" + search + "%' " +
                " OR " + " TEMP.DONG_NAME LIKE '%" + search + "%' " +
                ")"
                );
            if (XiaoQuSeqid > 0)
            {
                where.Append(" AND TEMP.XIAOQU_SEQID =  " + XiaoQuSeqid);
            }
            if (DongSeqid > 0)
            {
                where.Append(" AND TEMP.DONG_SEQID =  " + DongSeqid);
            }

            if (this.dateTimePicker1.Checked)
            {
                where.Append(" AND FEE_SHAOYAJING.OPERA_TIME >=  '" + this.dateTimePicker1.Value.ToString(StringUtils.YYYY_MM_DD) + "'");
            }
            if (this.dateTimePicker2.Checked)
            {
                where.Append(" AND FEE_SHAOYAJING.OPERA_TIME <= '" + this.dateTimePicker1.Value.ToString(StringUtils.YYYY_MM_DD) + "'");
            }
            //this.STATUS.DataSource = Constant.HOUSE_ROOM_STATUS_LIST;

            PageDataSet ds = dal.GetListByPage(where.ToString(), " RECORD_TIME DESC ", CURRENT_PAGE, Manager.getInstance().pageSize);
            this.dataGridView1.DataSource = ds.DataSet.Tables[0];

            
            this.userControlCommonListPage1.Inits(ds.AllRecord, this.CURRENT_PAGE);


        }

        private void UserControlFeeShaoYajing_Load(object sender, EventArgs e)
        {
            refreshData(this.CURRENT_PAGE);
        }

        private void button1Add_Click(object sender, EventArgs e)
        {
            FormFeeShaoYajingUpdate fu = new FormFeeShaoYajingUpdate();
            fu.oneRefreshParentData = refreshData;
            fu.ShowDialog(this);
        }

        private void button2Update_Click(object sender, EventArgs e)
        {
           

            if (dataGridView1.CurrentRow!=null)
            {

                String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();

                // this.textBox1.Text = value;
                FormFeeShaoYajingUpdate fu = new FormFeeShaoYajingUpdate();
                fu._ModelFEE_SHAOYAJING = dal.GetModel(Convert.ToInt32(seqid));
                fu.oneRefreshParentData = refreshData;

                fu.ShowDialog(this);

            }
            else
            {
                MessageBox.Show("请选择要更新记录.", "提示");
            }
        }

        private void button3Delete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow!=null)
            {
                String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();
                int seqidInt = Convert.ToInt32(seqid);
                if (seqidInt > 0)
                {


                    DialogResult dialogResult = MessageBox.Show("请确认是否要删除,删除押金记录将同时删除退押金的相关记录", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                    if (dialogResult.Equals(DialogResult.OK))
                    {
                        //bllRENT_CONTRACT.Delete(seqidInt);

                        aBllFEE_SHAOYAJING.deleteYajing(seqidInt);
                        //dal.Delete(seqidInt);
                        //刷新数据
                        refreshData(CURRENT_PAGE);
                    }


                }
            }
            else
            {
                MessageBox.Show("请选择要删除的记录.", "提示");
            }
        }

        private void button4Select_Click(object sender, EventArgs e)
        {
            this.refreshData(this.CURRENT_PAGE);
        }
        
        private void selectedTreeinfo(TreeInfo treeinfo)
        {
            if (treeinfo.NodeType == 0)
            {
                this.XiaoQuSeqid = 0;
                this.DongSeqid = 0;
            }
            else if (treeinfo.NodeType == 1)
            {
                this.XiaoQuSeqid = treeinfo.Id;
                this.DongSeqid = 0;
            }
            else if (treeinfo.NodeType == 2)
            {
                this.XiaoQuSeqid = 0;
                this.DongSeqid = treeinfo.Id;
            }
            
            this.refreshData(this.CURRENT_PAGE);
        }

        private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
        {
            refreshDataTuiyajing();
        }

        
        private void refreshDataTuiyajing() {

            if (this.dataGridView1.SelectedCells.Count == 0)
            {
                return;
            }

            String yajing_seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();
            if (yajing_seqid == null || yajing_seqid.Equals(""))
            {
                // MessageBox.Show("请先选择要管理的房间.");
                return;
            }
            DataSet ds = aDalFEE_TUIYAJING.GetList(" RENT_DEOSIT_FEE_SEQID  =  " + Convert.ToInt32(yajing_seqid));
            this.dataGridView2.DataSource = ds.Tables[0];
        }
        //退 加
        private void button2_Click(object sender, EventArgs e)
        {
            String yajing_seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();
            if (yajing_seqid == null || yajing_seqid.Equals(""))
            {
                // MessageBox.Show("请先选择要管理的房间.");
                return;
            }
           Decimal [] infos =  aBllFEE_SHAOYAJING.getNeedTuiYajing(Convert.ToInt32(yajing_seqid));
           if (infos[0] > infos[1])
           {
               FormTuiYajingUpdate fu = new FormTuiYajingUpdate();
               fu.oneRefreshParentData = refreshDataTuiyajing;
               fu.YAJING_MONEY = infos[0];
               fu.YAJING_SEQID = Convert.ToInt32(yajing_seqid);
               fu.ShowDialog(this);
           }
           else {
               MessageBox.Show("此押金已经退完....", "提示");
           }

        }
        //退 修
        private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show("退押金不提供更新操作,如需更新.请删除后再增加.....", "提示");
        }
        //退 删除
        private void button1_Click(object sender, EventArgs e)
        {
            String yajing_seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();
            if (yajing_seqid == null || yajing_seqid.Equals(""))
            {
                // MessageBox.Show("请先选择要管理的房间.","提示");
                return;
            }
            int yajing_seqidInt = Convert.ToInt32(yajing_seqid);

            String tuiyajing_seqid = dataGridView2.Rows[dataGridView2.CurrentRow.Index].Cells["SEQID_"].Value.ToString();
            int seqidInt = Convert.ToInt32(tuiyajing_seqid);
                if (seqidInt > 0)
                {

                    DialogResult dialogResult = MessageBox.Show("请确认是否要删除,删除后押金的状态将更新为[已退部分]", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                    if (dialogResult.Equals(DialogResult.OK))
                    {


                        aBllFEE_SHAOYAJING.deleteTuiYajing(seqidInt, yajing_seqidInt);
                        
                        //刷新数据
//                        this.dataGridView1
                        this.refreshData(this.CURRENT_PAGE);
                        refreshDataTuiyajing();
                       
                    }


                }
            
            else
            {
                MessageBox.Show("请选择要删除的记录.", "提示");
            }
        }
        /// <summary>
        /// 又击更新押金
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            this.button2Update_Click(null, null);
        }

        private void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            this.button3_Click(null, null);
        }
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }
        }
        private void dataGridView2_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView2.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }
        }

        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            setStatusWithDataGrid(dataGridView1);
        }
        public void setStatusWithDataGrid(DataGridView dgv)
        {
            for (int a = 0; a < dgv.Rows.Count; a++)
            {

                try
                {

                    int status = (int)dgv.Rows[a].Cells["STATUS"].Value;

                    DataGridViewTextBoxCell dgvcc = (DataGridViewTextBoxCell)dgv.Rows[a].Cells["STATUS_STR"];
                    dgvcc.Value = Constant.getByYajingStatus(status).Name;
                }
                catch (Exception)
                {

                }
            }
        }
        

    }
}
